package com.example.shutdown_v1.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import jakarta.servlet.http.HttpServletRequest;
import java.util.Map;

@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResponseEntity<Map<String, Object>> handleException(Exception e, HttpServletRequest request) {
        String uri = request != null ? request.getRequestURI() : "";
        String method = request != null ? request.getMethod() : "";
        log.error("HTTP请求异常: {} {}", method, uri, e);
        return ResponseEntity.internalServerError().body(Map.of(
                "success", false,
                "message", e.getMessage() != null ? e.getMessage() : "服务器内部错误",
                "path", uri
        ));
    }
}
